/*
 *  Copyright 2012 Ricardo Engel
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package org.mookt.quantity.money;

import java.math.BigDecimal;
import java.math.RoundingMode;

import org.mookt.quantity.Quantifiable;
import org.mookt.quantity.Quantity;
import org.mookt.unit.Unit;

import com.google.common.base.Objects;

/**
 * An amount of money with the standard decimal places defined by the currency.
 * 
 * <p>
 * This class represents a quantity of money with its amount specified in a
 * single {@link Unit currency}.
 * </p>
 * 
 * <p>
 * Every currency has a certain standard number of decimal places. This is
 * typically 2 (Euro, British Pound, US Dollar) but might be 0 (Japanese Yen), 1
 * (Vietnamese Dong) or 3 (Bahrain Dinar). The {@code Money} class is fixed to
 * this number of decimal places.
 * </p>
 * 
 * This class is immutable and thread-safe.
 * 
 * @author Ricardo Engel
 */
public class Money implements Quantifiable<Money> {

    public static Money of(final BigDecimal amount, final Unit unit) {
        return new Money(amount, unit);
    }

    public static Money of(final double amount, final Unit unit) {
        return of(BigDecimal.valueOf(amount), unit);
    }

    public static Money zero(final Unit unit) {
        return of(BigDecimal.ZERO, unit);
    }

    private static Money of(final Quantity quantity) {
        return of(quantity.getAmount(), quantity.getUnit());
    }

    private final Quantity quantity;

    private Money(final BigDecimal amount, final Unit currency) {
        quantity = Quantity.of(amount, currency);
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.mookt.unit.Quantifiable#getAmount()
     */
    public BigDecimal getAmount() {
        return quantity.getAmount();
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.mookt.unit.Quantifiable#getUnit()
     */
    public Unit getUnit() {
        return quantity.getUnit();
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.mookt.unit.Quantifiable#compareTo(java.lang.Object)
     */
    public int compareTo(final Money o) {
        return quantity.compareTo(o.quantity);
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.mookt.unit.Quantifiable#isZero()
     */
    public boolean isZero() {
        return quantity.isZero();
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.mookt.unit.Quantifiable#isPositive()
     */
    public boolean isPositive() {
        return quantity.isPositive();
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.mookt.unit.Quantifiable#isNegative()
     */
    public boolean isNegative() {
        return quantity.isNegative();
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.mookt.unit.Quantifiable#isGreaterThan(java.lang.Object)
     */
    public boolean isGreaterThan(final Money other) {
        return quantity.isGreaterThan(other.quantity);
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.mookt.unit.Quantifiable#isSmallerThan(java.lang.Object)
     */
    public boolean isSmallerThan(final Money other) {
        return quantity.isSmallerThan(other.quantity);
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.mookt.unit.Quantifiable#plus(java.math.BigDecimal)
     */
    public Money plus(final BigDecimal amount) {
        return of(quantity.plus(amount));
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.mookt.unit.Quantifiable#plus(double)
     */
    public Money plus(final double amount) {
        return of(quantity.plus(amount));
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.mookt.unit.Quantifiable#plus(java.lang.Object)
     */
    public Money plus(final Money other) {
        return of(quantity.plus(other.quantity));
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.mookt.unit.Quantifiable#minus(java.math.BigDecimal)
     */
    public Money minus(final BigDecimal amount) {
        return of(quantity.minus(amount));
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.mookt.unit.Quantifiable#minus(double)
     */
    public Money minus(final double amount) {
        return of(quantity.minus(amount));
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.mookt.unit.Quantifiable#minus(java.lang.Object)
     */
    public Money minus(final Money other) {
        return of(quantity.minus(other.quantity));
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.mookt.unit.Quantifiable#opposite()
     */
    public Money opposite() {
        return of(quantity.opposite());
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.mookt.unit.Quantifiable#times(java.math.BigDecimal)
     */
    public Money times(final BigDecimal amount) {
        return of(quantity.times(amount));
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.mookt.unit.Quantifiable#times(double)
     */
    public Money times(final double amount) {
        return of(quantity.times(amount));
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.mookt.unit.Quantifiable#dividedBy(java.math.BigDecimal)
     */
    public Money dividedBy(final BigDecimal amount) {
        return of(quantity.dividedBy(amount));
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.mookt.unit.Quantifiable#dividedBy(double)
     */
    public Money dividedBy(final double amount) {
        return of(quantity.dividedBy(amount));
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.mookt.unit.Quantifiable#round(int, java.math.RoundingMode)
     */
    public Money round(final int decimalPlaces, final RoundingMode roundingMode) {
        return of(quantity.round(decimalPlaces, roundingMode));
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.mookt.unit.Quantifiable#pow(int)
     */
    public Money pow(final int n) {
        return of(quantity.pow(n));
    }

    /*
     * (non-Javadoc)
     * 
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
        return quantity.getUnit().getSymbol() + quantity.getAmount().toString();
    }

    /*
     * (non-Javadoc)
     * 
     * @see java.lang.Object#equals(java.lang.Object)
     */
    @Override
    public boolean equals(final Object obj) {
        if (obj instanceof Money) {
            final Money that = (Money) obj;
            return quantity.equals(that.quantity);
        } else {
            return false;
        }
    }

    /*
     * (non-Javadoc)
     * 
     * @see java.lang.Object#hashCode()
     */
    @Override
    public int hashCode() {
        return Objects.hashCode(quantity);
    }
}
